import { BActor } from "../Core/BActor";
import { ACTOR_TYPE } from "../index";

import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor";
import vtkMapper from "@kitware/vtk.js/Rendering/Core/Mapper";
import vtkSphereSource from "@kitware/vtk.js/Filters/Sources/SphereSource";
import { Vector3 } from "@kitware/vtk.js/types";

export interface PointerActorOptions {
  color?: number[];
  radius?: number;
}

export class PointerActor extends BActor {
  readonly type = ACTOR_TYPE.GRAPHIC_ACTOR;
  createdBy = "default";
  instance: vtkActor = vtkActor.newInstance();
  mapper: vtkMapper = vtkMapper.newInstance();
  source: vtkSphereSource = vtkSphereSource.newInstance();
  private _config: PointerActorOptions = {};
  private _center: Vector3;

  userdata: any = {};

  constructor(center: Vector3, opts: PointerActorOptions = {}) {
    super();
    this._config = opts;
    this._center = center;
    this.source.setCenter(center);
    this.source.setRadius(opts.radius || 1);
    this.mapper.setInputData(this.source.getOutputData());
    this.instance.setMapper(this.mapper);
    if (opts.color) {
      const [r, g, b] = opts.color || [0, 0, 1];
      this.instance.getProperty().setColor(r, g, b);
    }
  }

  set position(v: Vector3) {
    this.instance.setPosition(...v);
    this._center = [...v];
  }

  get position(): Vector3 {
    return [...(this.instance.getPosition() as Vector3)];
  }

  get center(): Vector3 {
    return [...this._center];
  }
  clone() {
    const config: PointerActorOptions = {};
    if (this._config.color) {
      config.color = [...this._config.color];
    }
    if (this._config.radius) {
      config.radius = this._config.radius;
    }
    return new PointerActor([...this.position], config);
  }

  set visible(v: boolean) {
    this.instance.setVisibility(v);
  }

  get visible(): boolean {
    return this.instance.getVisibility();
  }
}
